// an order 1 decoder
// For a 8 speaker array

desc: Wig_1o_Oct2 WigWare Ambi Order 1, 1H0V 8 Speaker Decoder

//include Ambisonic Filter Functions
import WigAmbFilter.jsfx-inc

//include decoder filenames
filename:0,Wig_1o_Oct2_pic_1.png
filename:1,Wig_1o_Oct2_pic_2.png
filename:2,Wig_1o_Oct2_pic_3.png
filename:3,UoD_Logo_Small.png

slider1:0<0,2,1{Strict,Cardioid,Energy}>LF Decode Type
slider2:2<0,2,1{Strict,Cardioid,Energy}>HF Decode Type
slider3:400<100,1000,20>Transistion Frequency (Hz)
slider4:LFGain=0.0<-6.0,6.0,0.05>LF Decoder Gain (dB)
slider5:HFGain=1.8799<-6.0,6.0,0.05>HF Decoder Gain (dB)
slider6:DistFiltEnable=0<1,2,1{Off,Speaker Distance Comp (SDC),SDC & NFC}>Distance Filtering
slider7:SpDist=4<0.5,4,0.05>Speaker Distance
slider8:NFCDist=4<0.5,4,0.05>Near-Field Comp Distance

in_pin:0_AmbiX
in_pin:1_AmbiX
in_pin:2_AmbiX
in_pin:3_AmbiX
out_pin:22.5 az 0 el
out_pin:67.5 az 0 el
out_pin:112.5 az 0 el
out_pin:157.5 az 0 el
out_pin:202.5 az 0 el
out_pin:247.5 az 0 el
out_pin:292.5 az 0 el
out_pin:337.5 az 0 el
@init
bpos=0;
csp1 = 1024;
length_of_csps = 4;
end_of_csps = csp1 + length_of_csps;
csp2 = end_of_csps;
end_of_csps = csp2 + length_of_csps;
csp3 = end_of_csps;
end_of_csps = csp3 + length_of_csps;
csp4 = end_of_csps;
end_of_csps = csp4 + length_of_csps;
csp5 = end_of_csps;
end_of_csps = csp5 + length_of_csps;
csp6 = end_of_csps;
end_of_csps = csp6 + length_of_csps;
csp7 = end_of_csps;
end_of_csps = csp7 + length_of_csps;
csp8 = end_of_csps;
end_of_csps = csp8 + length_of_csps;
csp1[0] = 0.1250000000;csp1[1] = 0.0956708581;csp1[2] = 0.0000000000;csp1[3] = 0.2309698831;
csp2[0] = 0.1250000000;csp2[1] = 0.2309698831;csp2[2] = 0.0000000000;csp2[3] = 0.0956708581;
csp3[0] = 0.1250000000;csp3[1] = 0.2309698831;csp3[2] = 0.0000000000;csp3[3] = -0.0956708581;
csp4[0] = 0.1250000000;csp4[1] = 0.0956708581;csp4[2] = 0.0000000000;csp4[3] = -0.2309698831;
csp5[0] = 0.1250000000;csp5[1] = -0.0956708581;csp5[2] = 0.0000000000;csp5[3] = -0.2309698831;
csp6[0] = 0.1250000000;csp6[1] = -0.2309698831;csp6[2] = 0.0000000000;csp6[3] = -0.0956708581;
csp7[0] = 0.1250000000;csp7[1] = -0.2309698831;csp7[2] = 0.0000000000;csp7[3] = 0.0956708581;
csp8[0] = 0.1250000000;csp8[1] = -0.0956708581;csp8[2] = 0.0000000000;csp8[3] = 0.2309698831;

MaxRvGain2d = end_of_csps;
length_of_array = 2;
end_of_gain = MaxRvGain2d + length_of_array;
MaxRvGain2d[0] = 1.000;MaxRvGain2d[1] = 1.000;
MaxRvGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxRvGain3d + length_of_array;
MaxRvGain3d[0] = 1.000;MaxRvGain3d[1] = 1.000;
InPhase2d = end_of_gain;
length_of_array = 2;
end_of_gain = InPhaseGain2d + length_of_array;
InPhaseGain2d[0] = 1.0000;InPhaseGain2d[1] = 0.5000;
InPhaseGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = InPhaseGain3d + length_of_array;
InPhaseGain3d[0] = 1.0000;InPhaseGain3d[1] = 0.3333;
MaxReGain2d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxReGain2d + length_of_array;
MaxReGain2d[0] = 1.0000;MaxReGain2d[1] = 0.7071;
MaxReGain3d = end_of_gain;
length_of_array = 2;
end_of_gain = MaxReGain3d + length_of_array;
MaxReGain3d[0] = 1.0000;MaxReGain3d[1] = 0.5774;

BF = end_of_gain;
length_of_array = 4;
end_of_array = BF + length_of_array;
function SetDistCoef(dNFC,dspeak) local(w1,w2)
(
	NFCDist == 4.0? w1 = 0 :(
		w1 = 342.0/(dNFC*srate);
	);
	w2 = 342.0/(dspeak*srate);
	NF1.NF_Filt1Init(w1,w2,1.0);
	NF2.NF_Filt1Init(w1,w2,1.0);
	NF3.NF_Filt1Init(w1,w2,1.0);
);

SF0.shelfinit(srate,1200.0,1.0,1.0,1);
SF1.shelfinit(srate,1200.0,1.0,1.0,1);
SF2.shelfinit(srate,1200.0,1.0,1.0,1);
SF3.shelfinit(srate,1200.0,1.0,1.0,1);
NF1.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);
NF2.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);
NF3.NF_Filt1Init(342.0/(NFCDist*srate),342.0/(SpDist*srate),1.0);

@slider
slider1==0 ? GainTypeLF = MaxRvGain2d;
slider1==1 ? GainTypeLF = InPhaseGain2d;
slider1==2 ? GainTypeLF = MaxReGain2d;
slider2==0 ? GainTypeHF = MaxRvGain2d;
slider2==1 ? GainTypeHF = InPhaseGain2d;
slider2==2 ? GainTypeHF = MaxReGain2d;

LFGainLin = 2 ^(LFGain/6);
HFGainLin = 2 ^(HFGain/6);

SF0.shelfinit(srate,slider3,GainTypeLF[0]*LFGainLin,GainTypeHF[0]*HFGainLin,0);
SF1.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SF2.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SF3.shelfinit(srate,slider3,GainTypeLF[1]*LFGainLin,GainTypeHF[1]*HFGainLin,0);
SetDistCoef(NFCDist,SpDist);

@sample
BF[0] = SF0.shelf(spl0);
BF[1] = SF1.shelf(spl1);
BF[2] = SF2.shelf(spl2);
BF[3] = SF3.shelf(spl3);

DistFiltEnable!=0? (
	DistFiltEnable==1?(
		BF[1]=NF1.NF_Filt1ProcessSpDist(BF[1]);
		BF[2]=NF2.NF_Filt1ProcessSpDist(BF[2]);
		BF[3]=NF3.NF_Filt1ProcessSpDist(BF[3]);
	) : (
		BF[1]=NF1.NF_Filt1Process(BF[1]);
		BF[2]=NF2.NF_Filt1Process(BF[2]);
		BF[3]=NF3.NF_Filt1Process(BF[3]);
	);
);

spl0=spl1=spl2=spl3=spl4=spl5=spl6=spl7=0.0;

cnt = 0;
loop(4,
spl0+=BF[cnt]*csp1[cnt];
spl1+=BF[cnt]*csp2[cnt];
spl2+=BF[cnt]*csp3[cnt];
spl3+=BF[cnt]*csp4[cnt];
spl4+=BF[cnt]*csp5[cnt];
spl5+=BF[cnt]*csp6[cnt];
spl6+=BF[cnt]*csp7[cnt];
spl7+=BF[cnt]*csp8[cnt];
cnt = cnt+1;
);

@gfx 724 310
gfx_r = 1.0;gfx_g =1.0;gfx_b = 1.0;
gfx_x = 3;
gfx_y = 3;
gfx_drawnumber(GainTypeLF[0]*LFGainLin,3);gfx_drawstr(" : ");
gfx_drawnumber(GainTypeLF[1]*LFGainLin,3);gfx_drawstr(" LF Per Order Gain Values");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawnumber(GainTypeHF[0]*HFGainLin,3);gfx_drawstr(" : ");
gfx_drawnumber(GainTypeHF[1]*HFGainLin,3);gfx_drawstr(" HF Per Order Gain Values");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawstr("Sp Dist Comp set to: ");
DistFiltEnable>=1?(gfx_drawnumber(SpDist+0.005,2);gfx_drawchar('m');) : gfx_drawstr("off");
gfx_x = 3;
gfx_y += gfx_texth+3;
gfx_drawstr("NFC set to: ");
DistFiltEnable==2?(gfx_drawnumber(NFCDist+0.005,2);gfx_drawchar('m');) : gfx_drawstr("off");

gfx_x = 0;
gfx_y+=gfx_texth+3;
gfx_blit(0, 1.0, 0.0);
gfx_x+=245;
gfx_blit(1,1.0,0.0);
gfx_x+=245;
gfx_blit(2,1.0,0.0);
gfx_x = 600;
gfx_y = 5;
gfx_blit(3, 1.0, 0.0);
